package com.yoomiito.app.widget;

import android.content.Context;
import android.content.res.TypedArray;

import androidx.annotation.Nullable;

import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yoomiito.app.R;
import com.yoomiito.app.model.gift.GiftType;
import com.yoomiito.app.utils.YMToast;

import java.util.List;

public class TabView extends LinearLayout implements View.OnClickListener {

    private static final int DefaultIc = R.drawable.arrow_gray;
    private static final int DefaultDownIc = R.drawable.arrow_gray_down;
    private static final int DefaultUpIc = R.drawable.arrow_gray_up;
    private static final int DefaultColor = R.color.color_text;
    private static final int DefaultSelectColor = R.color.color_ff5100;

    Context mContext;
    private TextView defaultTv;
    private ImageView priceIv;
    private ImageView sizeIv;
    private ImageView couponIv;
    private boolean isPriceSelect = false;
    private boolean isSizeSelect = false;
    private boolean isCouponSelect = false;
    private OnChangeListener mOnChangeListener;
    private OnSortClickListener mOnSortClickListener;
    private int mDefaultIc;
    private int mUpIc;
    private int mDownIc;
    private int mDefaultColor;
    private int mSelectColor;
    private String mPriceFirstName = "price";//sale_price
    private String mDownName = "_des";
    private String mUpName = "_asc";
    private String mSaleFirstName = "total_sales"; //volume
    private List<GiftType> mGiftTypes;
    private View mDivView;
    int sort = 0;
    private  int salesSort = 0;
    int  categorizeId = 0;
    private TextView sortTv;
    private LinearLayout couponLl;
    private SortPopupWindow sortPopupWindow;

    public TabView(Context context) {
        this(context, null);
    }

    public TabView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TabView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public TabView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext = context;
        initAttr(attrs);
        initView();
    }

    private void initAttr(AttributeSet attrs) {
        TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.TabView);
        mDefaultIc = ta.getResourceId(R.styleable.TabView_default_ic, DefaultIc);
        mDownIc = ta.getResourceId(R.styleable.TabView_down_ic, DefaultDownIc);
        mUpIc = ta.getResourceId(R.styleable.TabView_up_ic, DefaultUpIc);
        mDefaultColor = ta.getColor(R.styleable.TabView_default_color, mContext.getResources().getColor(DefaultColor));
        mSelectColor = ta.getColor(R.styleable.TabView_select_color, mContext.getResources().getColor(DefaultSelectColor));
        ta.recycle();
    }

    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_toolbar_, null);
        defaultTv = view.findViewById(R.id.item_toolbar_tv);
        defaultTv.setTextColor(mDefaultColor);
        mDivView = view.findViewById(R.id.div);
        View priceView = view.findViewById(R.id.item_price);
        View sizeView = view.findViewById(R.id.item_size);
        priceIv = priceView.findViewById(R.id.item_price_change_iv);
        sizeIv = sizeView.findViewById(R.id.item_price_change_iv);
        TextView sizeTv = sizeView.findViewById(R.id.item_price_tv);
        sizeTv.setText("销量");

        couponLl = view.findViewById(R.id.item_coupon);
        couponIv = view.findViewById(R.id.item_coupon_iv);
        sortTv = view.findViewById(R.id.item_sort);
        sortTv.setTextColor(mDefaultColor);
        couponLl.setOnClickListener(this);
        sortTv.setOnClickListener(this);
        defaultTv.setOnClickListener(this);
        priceView.setOnClickListener(this);
        sizeView.setOnClickListener(this);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);
        addView(view);

        sortPopupWindow = new SortPopupWindow(mContext);
        sortPopupWindow.init();
        sortPopupWindow.setAnchorView(sortTv);
        sortPopupWindow.setOnSortSelectListener(giftType -> {
            if (mOnSortClickListener != null) {
                mOnSortClickListener.onClick(giftType);
            }
        });
    }

    int hasCoupon = 0;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.item_toolbar_tv:
                defaultTv.setTextColor(mSelectColor);
                priceIv.setImageResource(mDefaultIc);
                sizeIv.setImageResource(mDefaultIc);
                sort = 0;
                salesSort = 0;
                categorizeId = 0;
                break;
            case R.id.item_price:
                defaultTv.setTextColor(mDefaultColor);
                sizeIv.setImageResource(mDefaultIc);
                isPriceSelect = !isPriceSelect;
                priceIv.setImageResource(isPriceSelect ? mDownIc : mUpIc);
                sort = isPriceSelect ? 1 : 2;
                break;
            case R.id.item_size:
                defaultTv.setTextColor(mDefaultColor);
                priceIv.setImageResource(mDefaultIc);
                isSizeSelect = !isSizeSelect;
                sizeIv.setImageResource(isSizeSelect ? mDownIc : mUpIc);
//                sort = mSaleFirstName + (isSizeSelect ? mDownName : mUpName);
                salesSort = isSizeSelect ? 1 : 2;
                break;
            case R.id.item_coupon:
                isCouponSelect = !isCouponSelect;
                couponIv.setImageResource(isCouponSelect ? R.drawable.select_red_2 : R.drawable.select_red);
                hasCoupon = isCouponSelect ? 1 : 0;
                break;
            case R.id.item_sort:
                if (mGiftTypes == null) {
                    YMToast.showText("暂无分类");
                    return;
                }
                sortPopupWindow.setData(mGiftTypes);
                sortPopupWindow.showPopWindow(sortTv);
                return;
        }
        if (mOnChangeListener != null) {
            mOnChangeListener.onChange(sort,salesSort, hasCoupon);
        }
    }

    public void setGiftTypes(List<GiftType> giftTypes) {
        mGiftTypes = giftTypes;
    }

    public TabView setPriceFirstName(String priceFirstName) {
        mPriceFirstName = priceFirstName;
        return this;
    }

    public TabView setDownName(String downName) {
        mDownName = downName;
        return this;
    }

    public TabView setUpName(String upName) {
        mUpName = upName;
        return this;
    }

    public TabView setSaleFirstName(String saleFirstName) {
        mSaleFirstName = saleFirstName;
        return this;
    }

    public TabView showSort() {
        sortTv.setVisibility(VISIBLE);
        couponLl.setVisibility(GONE);
        return this;
    }

    public TabView dismissCoupon() {
        couponLl.setVisibility(GONE);
        return this;
    }

    public TabView setSelectCoupon() {
        isCouponSelect = true;
        hasCoupon = 1;
        couponIv.setImageResource(R.drawable.select_red_2);
        return this;
    }

    public TabView setDefault() {
        defaultTv.setTextColor(mSelectColor);
        return this;
    }

    public void divVisible(boolean visible) {
        mDivView.setVisibility(visible ? VISIBLE : GONE);
    }

    public TabView setOnChangeListener(OnChangeListener onChangeListener) {
        mOnChangeListener = onChangeListener;
        return this;
    }

    public void setOnSortClickListener(OnSortClickListener onSortClickListener) {
        mOnSortClickListener = onSortClickListener;
    }

    public interface OnChangeListener {
        void onChange(int  sort, int salesSort,int hasCoupon);
    }

    public interface OnSortClickListener {
        void onClick(GiftType giftType);
    }

    public void setmPriceFirstName(String mPriceFirstName) {
        this.mPriceFirstName = mPriceFirstName;
    }

    public void setmSaleFirstName(String mSaleFirstName) {
        this.mSaleFirstName = mSaleFirstName;
    }
}
